package com.sc.nft.admin.request;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.sc.nft.enums.MarketTypeEnum;
import com.sc.nft.exception.GlobalRunTimeException;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.hibernate.validator.constraints.Range;

import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.Date;

@Data
public class EditSecondCollectionRequest extends IdRequest {
    @ApiModelProperty("藏品Id")
    @NotNull(message = "藏品Id不能为空")
    private Long collectionId;

    @ApiModelProperty("起始价格")
    private BigDecimal startPrice;

    @ApiModelProperty("增长率")
    private BigDecimal upRatio;

    @ApiModelProperty("手续费")
    @NotNull(message = "手续费不能为空")
    @Range(min = 1L, max = 100L, message = "手续费范围不正确")
    private BigDecimal charge;

    @ApiModelProperty("类型: BRAND_INCUBATION:品牌孵化  FREE_MARKET:自由市场")
    @NotNull(message = "类型不能为空")
    private MarketTypeEnum marketType;

    @ApiModelProperty("孵化周期")
    private Integer incubationCycle;

    @ApiModelProperty("是否开启拆配 0:否 1:是")
    private Boolean isDisassembly;

    @ApiModelProperty("拆配倍数")
    private Integer disassemblyMultiple;

    @ApiModelProperty("用户购买份额(藏品总量的百分比)")
    private Integer buyShares;

    @ApiModelProperty("寄售规则")
    private String consignExplanation;

    @ApiModelProperty("自由交易向上百分比")
    private BigDecimal freeUpRatio;

    @ApiModelProperty("自由交易向下百分比")
    private BigDecimal freeDownRatio;

    @ApiModelProperty("自由市场浮动价格")
    private BigDecimal floatingPrice;

    @ApiModelProperty("最小值")
    private BigDecimal minPrice;

    @ApiModelProperty("最大值")
    private BigDecimal maxPrice;

    @ApiModelProperty("共识区最小值")
    private BigDecimal consensusMinPrice;

    @ApiModelProperty("最大值")
    private BigDecimal consensusMaxPrice;

    @ApiModelProperty("分润模板Id")
    @NotNull(message = "分润模板Id不能为空")
    private Long shareProfitPanelId;


    @ApiModelProperty("是否锁仓")
    @NotNull(message = "请设置是否锁仓")
    private Boolean isLockedPosition;

    @ApiModelProperty("锁仓类型 0:省缺 1:时间模式 2:日期形式 时间模式是获取藏品后延后多久,日期形式是统一锁到对应的时间")
    private Integer lockedPositionType;

    @ApiModelProperty("锁仓市场小时")
    private Integer lockedPositionHour;

    @ApiModelProperty("锁仓到期日期")
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    private Date lockedPositionTime;

    @ApiModelProperty("是否为平台展示")
    private Boolean platformShow;

    @ApiModelProperty("是否为品牌岛展示")
    private Boolean brandShow;

    @ApiModelProperty("是否为企业展示")
    private Boolean firmShow;

    @ApiModelProperty("排序")
    private Integer sort;

    @ApiModelProperty("gas上链费")
    @NotNull(message = "gas上链费不能为空")
    private BigDecimal gas;

    @ApiModelProperty("是否开启求购")
    @NotNull(message = "是否开启求购不能为空")
    private Boolean enableWantBuy;

    @ApiModelProperty("最低求购价")
    private BigDecimal minWantBuyPrice;

    @ApiModelProperty("最高求购价")
    private BigDecimal maxWantBuyPrice;

    @ApiModelProperty("单次最高求购数量")
    private Integer wantBuyQuantity;

    @ApiModelProperty("求购持有条件")
    private Boolean enableWantBuyHoldCondition;

    @ApiModelProperty("求购消耗道具")
    private Boolean enableWantBuyConsumeProp;

    @ApiModelProperty("求购持有藏品id")
    private Long wantBuyHoldCollectionId;

    @ApiModelProperty("求购消耗道具id")
    private Long wantBuyConsumePropId;

    @ApiModelProperty("求购消耗道具数量")
    private Integer wantBuyConsumePropQuantity;


    public Boolean lockedPositionDecide() {
        // 是否为锁仓
        if (!this.isLockedPosition) {
            return true;
        }
        // 是否为锁仓
        if (this.isLockedPosition) {
            // 是锁仓但是是省缺不允许
            if (this.lockedPositionType.equals(0)) {
                throw new GlobalRunTimeException("请设置锁仓类型");
            }
            // 锁时长模式
            if (this.lockedPositionType.equals(1)) {
                // 锁仓市场大小
                if (this.lockedPositionHour > 0 && this.lockedPositionHour <= 99999) {
                    return true;
                }
            }
            // 锁日期模式
            if (this.lockedPositionType.equals(2)) {
                if (this.lockedPositionTime == null) {
                    throw new GlobalRunTimeException("请传入锁仓到期日期");
                }
                // 锁仓到期日期必须大于当前时间
                if (this.lockedPositionTime.getTime() > System.currentTimeMillis()) {
                    return true;
                }
            }
        }
        return false;
    }
}
